package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.system.domain.ConstractInstitute;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ConstractPurchase;
import com.ruoyi.system.service.IConstractPurchaseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购明细Controller
 * 
 * @author ruoyi
 * @date 2022-04-04
 */
@Controller
@RequestMapping("/system/purchase")
public class ConstractPurchaseController extends BaseController
{
    private String prefix = "system/purchase";

    @Autowired
    private IConstractPurchaseService constractPurchaseService;

    @RequiresPermissions("system:purchase:list")
    @GetMapping("{id}")
    public String purchase(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("constractId", id);
        return prefix + "/purchase";
    }

    /**
     * 查询采购明细列表
     */
    @RequiresPermissions("system:purchase:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ConstractPurchase constractPurchase)
    {
        startPage();
        List<ConstractPurchase> list = constractPurchaseService.selectConstractPurchaseList(constractPurchase);
        return getDataTable(list);
    }

    /**
     * 导出采购明细列表
     */
    @RequiresPermissions("system:purchase:export")
    @Log(title = "采购明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ConstractPurchase constractPurchase)
    {
        List<ConstractPurchase> list = constractPurchaseService.selectConstractPurchaseList(constractPurchase);
        ExcelUtil<ConstractPurchase> util = new ExcelUtil<ConstractPurchase>(ConstractPurchase.class);
        return util.exportExcel(list, "采购明细数据");
    }
    @PostMapping("/details/{id}")
    @ResponseBody
    public TableDataInfo list(@PathVariable("id") Long id)
    {
        ConstractPurchase branch = new ConstractPurchase();
        branch.setConstractId(id);
        startPage();
        return getDataTable(constractPurchaseService.selectConstractPurchaseList(branch));
    }
    /**
     * 新增采购明细
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("constractId", id);
        return prefix + "/add";
    }

    /**
     * 新增保存采购明细
     */
    @RequiresPermissions("system:purchase:add")
    @Log(title = "采购明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ConstractPurchase constractPurchase)
    {
        return toAjax(constractPurchaseService.insertConstractPurchase(constractPurchase));
    }

    /**
     * 修改采购明细
     */
    @RequiresPermissions("system:purchase:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ConstractPurchase constractPurchase = constractPurchaseService.selectConstractPurchaseById(id);
        mmap.put("constractPurchase", constractPurchase);
        return prefix + "/edit";
    }

    /**
     * 修改保存采购明细
     */
    @RequiresPermissions("system:purchase:edit")
    @Log(title = "采购明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ConstractPurchase constractPurchase)
    {
        return toAjax(constractPurchaseService.updateConstractPurchase(constractPurchase));
    }


    /**
     * 删除采购明细
     */
    @RequiresPermissions("system:purchase:remove")
    @Log(title = "采购明细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(constractPurchaseService.deleteConstractPurchaseByIds(ids));
    }
}
